<?php
/**
 * --------------------------------------------------------------
 * 转换函数库
 * Author: zd <kfzphp@126.com>
 * Date: 2018-06-01
 * Project: kfzphp.com
 * --------------------------------------------------------------
 */

/**
 * 字符集换转
 * @param  [type]     $string [description]
 * @return [type]             [description]
 */
function my_iconv($string)
{
    return iconv("UTF-8", "GB2312//IGNORE", $string);
}

/**
 * Object转Array
 * @param   [type]     $object [description]
 * @return  [type]             [description]
 */
function object_to_array($object)
{
    return json_decode(json_encode($object), true);
}

/**
 * xml转array
 * @param  [type]     $xml [description]
 */
function postxml_to_array($xml)
{
    if (!empty($xml)) {
        libxml_disable_entity_loader(true); // 禁止引用外部xml实体
        return json_decode(json_encode(simplexml_load_string($xml, 'SimpleXMLElement', LIBXML_NOCDATA)), true);
    } else {
        return false;
    }
}

/**
 * url参数字符串转数组
 * bid=243&weid=3&pay_openid=oWOWvwTd1OoNtiNfhp9mMyWXzT58&pay_use=1
 * @param  [type]     $query [description]
 * @return [type]            [description]
 */
function url_to_array($query)
{
    $queryParts = explode('&', $query);
    $params = array();
    foreach ($queryParts as $param) {
        $item = explode('=', $param);
        $params[$item[0]] = $item[1];
    }
    return $params;
}

/**
 * 关联数组转url拼接
 * @param  [type]     $array [description]
 * @return [type]            [description]
 */
function array_to_url($array, $splice = '&')
{
    $temp = [];
    foreach ($array as $key => $val) {
        $temp[] = $key . '=' . $val;
    }
    return implode($splice, $temp);
}

/**
 * 无格式时间转时间戳
 * @param  [type]     $datetime [格式：20141030133525]
 * @return [type]               [description]
 */
function datetime_to_timestamp($datetime)
{
    if (!empty($datetime)) {
        $str = substr($datetime, 0, 4) . '-' .
        substr($datetime, 4, 2) . '-' .
        substr($datetime, 6, 2) . ' ' .
        substr($datetime, 8, 2) . ':' .
        substr($datetime, 10, 2) . ':' .
        substr($datetime, 12, 2);
        return strtotime($str);
    } else {
        return false;
    }
}

/**
 * 商户处理后同步返回给微信支付参数
 * @param  [type]     $array [description]
 * @param  integer    $level [description]
 * @return [type]            [description]
 */
function wxpay_array_xml($array, $level = 1)
{
    $s = $level == 1 ? "<xml>" : '';
    foreach ($array as $tagname => $value) {
        if (is_numeric($tagname)) {
            $tagname = $value['TagName'];
            unset($value['TagName']);
        }
        if (!is_array($value)) {
            $s .= "<{$tagname}>" . (!is_numeric($value) ? '<![CDATA[' : '') . $value . (!is_numeric($value) ? ']]>' : '') . "</{$tagname}>";
        } else {
            $s .= "<{$tagname}>" . array2xml($value, $level + 1) . "</{$tagname}>";
        }
    }
    $s = preg_replace("/([\x01-\x08\x0b-\x0c\x0e-\x1f])+/", ' ', $s);
    return $level == 1 ? $s . "</xml>" : $s;
}
